package neuralNetwork;

import neuralNetwork.NeuralNetwork.ArrayOperations;

public class NeuralNetworkLayer {
	
	private Neuron[] neurons; 

	public NeuralNetworkLayer(NeuralNetworkLayer nextLayer, int nofNeurons) {
		this.neurons = new Neuron[nofNeurons]; 
		for (int i = 0; i < nofNeurons; i++) {
			neurons[i] = new Neuron(nextLayer); 
		}
	}

	public int nofWeights(){
		int nofWeights = 0; 
		for (Neuron neuron : neurons) {
			nofWeights += neuron.nofWeights(); 
		}
		return nofWeights; 
	}

	public double[] getWeights() {
		double[] weights = new double[nofWeights()];
		int index = 0; 
		for (Neuron neuron : neurons) {
			index = ArrayOperations.insertData(weights, neuron.getWeights(), index);
		}
		return weights;
	}

	public void setWeights(double[] selectData) {
		int index = 0;
		int size = 0; 
		for (Neuron neuron : neurons) {
			size = neuron.nofWeights(); 
			neuron.setWeights(ArrayOperations.selectData(selectData, index, size));
			index += size; 
		}
	}

	public Neuron get(int index){
		return neurons[index]; 
	}
	
	public int nofNeurons(){
		return neurons.length; 
	}
	
	public void fireUpdate(){
		for (Neuron neuron : neurons) {
			neuron.fireUpdate(); 
		}
	}
	
	public void setInput(double[] inputs){
		for (int i = 0; i < neurons.length; i ++) {
			neurons[i].setInput(inputs[i]); 
		}
	}
	
	public double[] returnOutput(){
		double[] outputs = new double[neurons.length]; 
		for (int i = 0; i < neurons.length; i++) {
			outputs[i] = neurons[i].returnOutput(); 
		}
		return outputs;
	}
	
	public String toString(){
		String theString = "";
		for (int i = 0; i < neurons.length; i++) {
			Neuron neuron = neurons[i];
			theString += neuron.toString() + ",   "; 
			
		}
		return theString; 
	}
}
